﻿function ChatRoomInfo(meetingRoomUrl, chatRoomSecurityGroupAdmin, chatRoomSecurityGroupMember, chatDescription, chatRoomAdmins, chatRoomMembers, roomName, sIP, id, chatRoomId) {
    this.meetingRoomUrl = meetingRoomUrl;
    this.chatRoomSecurityGroupAdmin = chatRoomSecurityGroupAdmin;
    this.chatRoomSecurityGroupMember = chatRoomSecurityGroupMember;
    this.chatDescription = chatDescription;
    this.chatRoomAdmins = chatRoomAdmins;
    this.chatRoomMembers = chatRoomMembers;
    this.roomName = roomName;
    this.sIP = sIP;
    this.id = id;
    this.chatRoomId = chatRoomId;
}

var currentRoomInfo = null;
var slCtl = null;
function UpdateLyncUserStatus(sip, previousCode, statusCode) {

}

    function SetErrorMessage(errorMessage) {    
    }
   
    function LoadLyncMeetingWindowLoad() {
        SP.SOD.executeFunc("sp.js", "SP.ClientContext", QueryChatRoom);   
    }

    function QueryChatRoom() {
        var clientContext = new SP.ClientContext();
        roomId = clientContext.$1P_0.substring(1);
        var chatRoomList = clientContext.get_site().get_rootWeb().get_lists().getByTitle('群组');
        var query = "  <View><Query>" +
                                             "<Where>" +
                                                  "<Eq>" +
                                                    "<FieldRef Name='ChatRoomID' />" +
                                                    "<Value Type='Text'>" + roomId + "</Value>" +
                                                  "</Eq>" +
                                                "</Where>" +
                                        "</Query></View>";

        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(query);
        currentRoomItem = chatRoomList.getItems(camlQuery);

        clientContext.load(currentRoomItem, 'Include(MeetingRoomUrl,ChatRoomSecurityGroupAdmin,ChatRoomSecurityGroupMember,ChatDescription,ChatRoomAdmins,ChatRoomMembers,RoomName,SIP,ID,ChatRoomID)');

        clientContext.executeQueryAsync(QueryChatRoomSuccessed, onQueryFailed);
    }

    function QueryChatRoomSuccessed() {
        var chatRoomEnumerator = currentRoomItem.getEnumerator();
        while (chatRoomEnumerator.moveNext()) {
            var chatroom = chatRoomEnumerator.get_current();
            var meetingRoomUrl = chatroom.get_item("MeetingRoomUrl");       
            var chatRoomSecurityGroupAdmin = chatroom.get_item("ChatRoomSecurityGroupAdmin");
            var chatRoomSecurityGroupMember = chatroom.get_item("ChatRoomSecurityGroupMember");
            var chatDescription = chatroom.get_item("ChatDescription");
            var chatRoomAdmins = chatroom.get_item("ChatRoomAdmins");            
            var chatRoomMembers = chatroom.get_item("ChatRoomMembers");          
            var roomName = chatroom.get_item("RoomName");
            var roomSIP = chatroom.get_item("SIP");
            var roomItemId = chatroom.get_item("ID");
            var chatRoomID = chatroom.get_item("ChatRoomID");          
            userIds = [];
            sips = [];       
            var roomSips = roomSIP.split(";");
            var usersSips = "";      
            for(var i=0; i<roomSips.length-1; i++)
            {
                var currentSIP = roomSips[i].split("#");            
                userIds.push(currentSIP[0]);
                sips.push(currentSIP[1]);
                usersSips += currentSIP[1] + "#";
            }
     
            currentRoomInfo = new ChatRoomInfo(meetingRoomUrl, chatRoomSecurityGroupAdmin, chatRoomSecurityGroupMember, chatDescription, chatRoomAdmins, chatRoomMembers, roomName, usersSips, roomItemId, chatRoomID);

            if (slCtl) {
                SP.SOD.executeFunc("sp.js", "SP.ClientContext", StartMeeting);
            }       
        }
    }

    function onQueryFailed(sender, args) {
        alert('error' + args.message);
    }

    function onSilverlightError(sender, args) {      
        document.getElementById('SilverlightHost').style.dispaly = 'none';
        console.log("Silverlight error:" + args);
    }

    function pluginLoaded(sender, args) {
        slCtl = sender.getHost();
        document.getElementById('SilverlightHost').style.dispaly = 'none';    

        if (window.currentRoomInfo && slCtl) {
            SP.SOD.executeFunc("sp.js", "SP.ClientContext", StartMeeting);
        }     
    }

    function GetSiteUrl() {
        var port = "";
        if (document.location.port && document.location.port.lenght > 0) {
            port = ":" + document.location.port;
        }

        return document.location.protocol + '//' + document.location.host + port;
    }

    function StartMeeting() {
        var clientContext = new SP.ClientContext();
        roomId = clientContext.$1P_0.substring(1)
        var urlGetConferenceMeeetingStatus = GetSiteUrl() + "/_layouts/hzWeb/GetConferenceMeeetingStatus.ashx?meetingId=" + roomId + "&timestamp=" + (new Date()).getTime();
        $.ajax({
            type: "GET",
            url: urlGetConferenceMeeetingStatus,
            dataType: "json",
            success: function (data) {
                slCtl.Content.SL2JS.SetLyncUsers(currentRoomInfo.sIP);
                slCtl.Content.SL2JS.SetMeetingUrl(currentRoomInfo.meetingRoomUrl);
                slCtl.Content.SL2JS.SetMeetingId(currentRoomInfo.chatRoomId);
                slCtl.Content.SL2JS.SetWebUrl(GetSiteUrl());
                slCtl.Content.SL2JS.StartMeeting(currentRoomInfo.meetingRoomUrl, currentRoomInfo.chatRoomId+ "#" +data.meetingRoomURLPrefix);
                setTimeout(function () {
                    window.close();
                }, 5000);
            }
        });
    }


    $(document).ready(function () {
        LoadLyncMeetingWindowLoad();
        document.title = "正在加载Lync meeting .....";
    });